
////////////////////////////////////////////////////////////////////////////////////////

#ifndef _COMACCESS_H_
#define _COMACCESS_H_

#include "DDAutoDelete.h"

class ComAccess 
{
public:
	ComAccess(VOID);
	ComAccess(char *lpszPortNum);
	~ComAccess() 
	{ 
		Close(); 
	}
public:
	// For more definitions see <winbase.h>
	BOOL Open(char *lpszPortNum	= "com1",
		DWORD  dwBaudRate  = CBR_57600, 
		BYTE   byParity    = EVENPARITY,
		BYTE   byStopBits  = TWOSTOPBITS,
		BYTE   byByteSize  = 7);
	BOOL Open(int nPortNum	= 0,
		DWORD  dwBaudRate  = CBR_57600, 
		BYTE   byParity    = EVENPARITY,
		BYTE   byStopBits  = TWOSTOPBITS,
		BYTE   byByteSize  = 7);

	VOID Close(VOID);
	int WriteData(LPCVOID pdata, DWORD len);
	int ReadData(LPVOID  pdest, DWORD len, DWORD dwMaxWait = 500);

	char *GetErrorMessage(VOID)
	{ 
		return m_lpszErrorMessage; 
	}

private:
	VOID ErrorToString(LPCSTR lpszMessage);
	BOOL IsNT(VOID);

public:
	HANDLE      m_hCom; // Device handle

private:
	OVERLAPPED  m_ov;   // A structure that contains informations which are
	                    // used for asynchronous input and output operations
	char       m_lpszErrorMessage[256];

private:
#define MAX_CLEAR_BUF 20*1024
	char m_cClearTmp[MAX_CLEAR_BUF];

private:
	DDCriticalSection m_Lock;

};


#endif // _COMACCESS_H_
